package com.itqf.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itqf.bean.DtsPermission;
import com.itqf.bean.DtsRole;
import com.itqf.dto.DtsPermissionDto;
import com.itqf.dto.DtsSearchDto;
import com.itqf.mapper.DtsPermissionMapper;
import com.itqf.mapper.DtsRoleMapper;
import com.itqf.service.DtsRoleService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * projectName: sm_project
 *
 * @author: 赵伟风
 * time: 2021/7/13 14:27
 * description:
 */
@Service
public class DtsRoleServiceImpl implements DtsRoleService {

    @Autowired
    private DtsRoleMapper dtsRoleMapper;

    @Autowired
    private DtsPermissionMapper dtsPermissionMapper;

    public static final Logger logger = LoggerFactory.getLogger(DtsRoleServiceImpl.class);

    /**
     * 根据角色id查询角色名称
     *
     * @param roleIds
     * @return
     */
    @Override
    public List<String> queryRoleNames(Integer[] roleIds) {

        if (roleIds == null || roleIds.length == 0) {

            logger.warn("[角色查询业务] roleIds为null!");

            return null;
        }

        List<String> roles = dtsRoleMapper.queryRoleNameByRoleIds(roleIds);

        return roles;
    }

    @Override
    public List<Map> findRoleNames() {

        List<Map> roleList =  dtsRoleMapper.queryRoleNames();

        logger.debug("[角色列表]业务查询数据为:{}", roleList);

        return roleList;
    }


    /**
     * dto vo bean pojo
     * @param searchDto
     * @return
     */
    @Override
    public Map queryRoleByPage(DtsSearchDto searchDto) {

        PageHelper.startPage(searchDto.getPage(), searchDto.getLimit());

        List<DtsRole> roles = dtsRoleMapper.queryRolesByPage(searchDto);

        PageInfo<DtsRole> pageInfo = new PageInfo<>(roles);

        //2.封装返回数据
        Map data = new HashMap();
        data.put("total", pageInfo.getTotal());
        data.put("items", pageInfo.getList());

        logger.info("[角色管理查询业务]调用结束!查询封装数据为:{}", data);

        return data;
    }

    @Override
    public boolean checkSuper(int roleId) {

        Long count = dtsRoleMapper.querySuper(roleId);

        logger.info("[检查超级管理员业务]调用结束,查询当前角色:{},对应管理员数据:{}", roleId,count);

        return count >= 1;
    }

    @Override
    public Set<String> queryPermissions(int roleId) {

        Set<String> permissions = dtsRoleMapper.queryPermissions(roleId);

        logger.info("[查询角色权限]查看角色id:{},对应的权限为:{}", roleId,permissions);

        return permissions;
    }

    @Override
    public void removePermission(int roleId) {

        dtsPermissionMapper.deleteByRoleId(roleId);

    }

    @Override
    public void save(DtsPermissionDto permissionDto) {

        for (String permission : permissionDto.getPermissions()) {
            DtsPermission dtsPermission = new DtsPermission();
            dtsPermission.setPermission(permission);
            dtsPermission.setRoleId(permissionDto.getRoleId());
            dtsPermission.setDeleted(false);
            dtsPermission.setAddTime(new Date());
            dtsPermissionMapper.insert(dtsPermission);
        }

    }
}
